import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JSplitPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class DialogSupprimerSalon extends JDialog {
	private JPasswordField passwordField;
	private JLabel label_1;
	private Client controleur;
	
	private JList list;
	
	public String[] salons;
	
	public DialogSupprimerSalon(Client client) throws SQLException, ClassNotFoundException {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				fermerFenetre();
			}
		});
		setModal(true);
		this.controleur = client;
		this.setBounds(150,150,451,314);
		setTitle("Supprimer un Salon");
		getContentPane().setLayout(null);
		
		this.salons = Connexion_BD.getSalons();

		list = new JList(salons);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				try {
					GererChampMdp();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		list.setBounds(12, 12, 418, 158);
		getContentPane().add(list);
		
		JLabel label = new JLabel("Liste des salons");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setBounds(12, 182, 418, 15);
		getContentPane().add(label);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setBounds(178, 249, 252, 27);
		getContentPane().add(splitPane);
		
		JButton btnFermer = new JButton("Fermer");
		btnFermer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fermerFenetre();
			}
		});
		splitPane.setLeftComponent(btnFermer);
		
		JButton btnSupprimer = new JButton("Supprimer");
		btnSupprimer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					supprimerSalon();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		splitPane.setRightComponent(btnSupprimer);
		
		label_1 = new JLabel("Mot de passe");
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		label_1.setEnabled(false);
		label_1.setBounds(12, 218, 117, 15);
		getContentPane().add(label_1);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(147, 216, 283, 19);
		getContentPane().add(passwordField);
	}
	
	public void activationPublic()
	{
		label_1.setEnabled(false);
		passwordField.setEnabled(false);
		passwordField.setText("");
	}
	
	public void activationPrive()
	{
		label_1.setEnabled(true);
		passwordField.setEnabled(true);
	}
	
	
	public void fermerFenetre()
	{
		this.controleur.fermerDialogSupprimerSalon();
	}
	
	private void supprimerSalon() throws SQLException, ClassNotFoundException {
		int num = list.getSelectedIndex();
		String salon_a_supprimer = null;
		String bon_mdp = null;
		String password = new String(passwordField.getPassword());
		
		salon_a_supprimer = this.salons[num];
		
		bon_mdp = Connexion_BD.getMdpSalon(salon_a_supprimer);


		if (! Connexion_BD.estPublic(salon_a_supprimer) && password.equals("")) 
		{
			JOptionPane.showMessageDialog(this, "Vous devez entrer le mot de passe du salon !", "Erreur : Mot de passe manquant", JOptionPane.ERROR_MESSAGE);
		}
		
		else if (! Connexion_BD.estPublic(salon_a_supprimer) && ! password.equals(bon_mdp))
		{
			JOptionPane.showMessageDialog(this, "Le mot de passe entré pour ce salon est incorrect !", "Erreur : Mot de passe incorrect", JOptionPane.ERROR_MESSAGE);
		}
		
		else if (this.controleur.supprimerSalon(salon_a_supprimer, password)) 
		{
			JOptionPane.showMessageDialog(this, "Le salon " + salon_a_supprimer + " a bien été supprimé !", "Confirmation suppresion du salon", JOptionPane.INFORMATION_MESSAGE);
			this.MajListSalons();
		}
		

		
	}
	
	private void MajListSalons() throws SQLException, ClassNotFoundException {
		salons = Connexion_BD.getSalons(); 
		list.setListData(salons);
	}
	
	private void GererChampMdp() throws SQLException, ClassNotFoundException {
		int num = list.getSelectedIndex();
		String salon_a_supprimer = this.salons[num];
		
		if (! Connexion_BD.estPublic(salon_a_supprimer)) 
		{
			this.activationPrive();
		}
		else if (Connexion_BD.estPublic(salon_a_supprimer))
		{
			this.activationPublic();
		}
		
	}
}
